package com.cn.web.controller.PartsManagement;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.vo.PartsManagement.BusinessDepartmentType;
import com.cn.bean.vo.PartsManagement.BusinessProjectTypeMapping;
import com.cn.bean.vo.PartsManagement.TeamPersonnel;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.PartsManagement.BusinessProjectTypeMappingDao;
import com.cn.service.server.PartsManagement.BusinessProjectTypeMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Btms")
@Transactional
public class BusinessProjectTypeMappingController {
    private final BusinessProjectTypeMappingService businessProjectTypeMappingService;

    @Autowired(required = false)
    public BusinessProjectTypeMappingController(BusinessProjectTypeMappingService businessProjectTypeMappingService) {
        this.businessProjectTypeMappingService = businessProjectTypeMappingService;
    }

    @RequestMapping("/select/BusinessDepartmentType")
    @ResponseBody
    public List<BusinessProjectTypeMapping> selectBusinessDepartmentType(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String name = jsonObject.getString("inputName");
        name = (name != null) ? name : "";
        return businessProjectTypeMappingService.selectBusinessProjectTypeMapping(page, limit, name);
    }

    @RequestMapping("/add/BusinessProjectTypeMapping")
    @ResponseBody
    public Result addBusinessProjectTypeMapping(@RequestBody JSONObject jsonObject) {
//        Integer ID = jsonObject.getInteger("ID");
        Integer businessTypeID = jsonObject.getInteger("businessTypeID");
        Integer itemType = jsonObject.getInteger("itemType");
        Integer businessProjectID = jsonObject.getInteger("businessProjectID");
        Integer status = jsonObject.getInteger("status");
        String description = jsonObject.getString("description");

        return businessProjectTypeMappingService.addBusinessProjectTypeMapping(businessTypeID, itemType, businessProjectID, description, status);
    }

    @RequestMapping("/delete/BusinessProjectTypeMapping")
    @ResponseBody
    public Result deleteProjectTypeMapping(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<BusinessProjectTypeMapping> idList = new ArrayList<>();
        for (int i = 0; i < deleteIdList.size(); i++) {
            JSONObject deleteIdObject = deleteIdList.getJSONObject(i);
            int id = deleteIdObject.getInteger("ID");
            BusinessProjectTypeMapping personnel = new BusinessProjectTypeMapping();
            personnel.setID(id);
            idList.add(personnel);
        }

        return businessProjectTypeMappingService.deleteBusinessProjectTypeMapping(idList);
    }

    @RequestMapping("/upDate/BusinessProjectTypeMapping")
    @ResponseBody
    public Result upDateBusinessProjectTypeMapping(@RequestBody JSONObject jsonObject) {
        Integer ID = jsonObject.getInteger("ID");
        Integer businessTypeID = jsonObject.getInteger("businessTypeID");
        Integer itemType = jsonObject.getInteger("itemType");
        Integer businessProjectID = jsonObject.getInteger("businessProjectID");
        Integer status = jsonObject.getInteger("status");
        String description = jsonObject.getString("description");

        return businessProjectTypeMappingService.upDateBusinessProjectTypeMapping(ID, businessTypeID, status, itemType, businessProjectID, description);
    }
}
